package foo.sample;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import foo.sample.model.Book;
import foo.sample.service.BookService;

public class XfireClientTest2 {
	public Log logger = LogFactory.getLog(getClass());
    static String addr = "http://127.0.0.1:8080/xfire-server/services/BookService";
//    static String addr = "http://127.0.0.1:8089/axis2-server/services/BookService";
    static String tns = "http://service.ws.sample.freedom";
  
	/**
	 * 不推荐此法：
	 * 需要严格遵守服务端的接口类及涉及实体类的定义；
	 * 且为匿名访问
	 */
	@Test
	public void call() {
		try {
			Service service = new ObjectServiceFactory().create(BookService.class);  
			XFireProxyFactory factory = new XFireProxyFactory();
			BookService client = (BookService)factory.create(service, addr); 
			Book serviceResponse = client.getBookById2(1);
			logger.info(serviceResponse);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public <T> T genObjByXml(Class<T> clazz, String xmlStr) throws JsonParseException, JsonMappingException, IOException
	{
		XmlMapper xmlMapper = new XmlMapper();
		T object = xmlMapper.readValue(xmlStr, clazz);
		return object;
	}

}
